﻿using System;
using System.Linq;
using System.Windows.Forms;
using EvidenceManagement.ViewModel.Common;
using EvidenceManagement.ViewModel.Security;
using EvidenceManagement.PresentationModel.Security;
using Autofac;
using Autofac.Core;

namespace EvidenceManagement.Workstation.Security
{
    public partial class frmLogin : Form, ILogOnView
    {

        #region Fields

        readonly ILogOnPresenter presenter;
        readonly IMasterView masterView = null;
        
        #endregion

        #region Constructor

        public frmLogin()
        {
            InitializeComponent();

            presenter = AutofacContainerBuilder.Container.Resolve<ILogOnPresenter>();
            //presenter = new LogOnPresenter(this);
#if DEBUG
            txtLoginPassword.Text = "LOCALSYSTEM";
            txtLoginUserName.Text = "LOCALSYSTEM";
#endif
        }

        public frmLogin(IMasterView masterView)
            : this()
        {
            this.masterView = masterView;
        }

        #endregion

        private void frmLogin_Load(object sender, EventArgs e)
        {
            presenter.OnLoad();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            presenter.LogOn();
        }

        #region ILoginView Members

        public event EventHandler OnLogOnPassed;

        public string UserName
        {
            get { return txtLoginUserName.Text.Trim(); }
        }

        public string Password
        {
            get { return txtLoginPassword.Text.Trim(); }
            set { txtLoginPassword.Text = value; }
        }

        public void LogOnFailed()
        {
            MessageBox.Show("Username and password combination is invalid.", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        public void LogOnPassed()
        {
            if (OnLogOnPassed != null)
                OnLogOnPassed(this, new EventArgs());
        }

        #endregion

        #region IView Members

        public Guid CurrentSystemUserId
        {
            get;
            set;
        }

        public void ShowView()
        {
            this.Show();
        }

        public void AbortView(string abortReason)
        {
        }

        public IMasterView MasterView
        {
            get
            {
                return masterView;
            }
        }

        public void CloseView()
        {
            presenter.OnClose();
            this.Close();
        }
        
        #endregion
       
    }
}